package testClasses.xstream;

import gameAppLayer.enums.Constants;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.util.logging.Logger;

import model.AbstractModel;

import com.thoughtworks.xstream.XStream;

import testClasses.ATest;
import testClasses.TestingSystem;
import utilities.Key;

public class XStreamLoadSaveTest extends ATest {

	private static final Logger logger = Logger.getLogger(XStreamLoadSaveTest.class.getName());
	
	public XStreamLoadSaveTest(TestingSystem system) {
		super(system);
		this.name = "XStreamLoadSaveTest";
	}

	@Override
	public boolean execute() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		
		
		
		File file;
		
		XStream xstream = new XStream();		
		
		//register aliases
		xstream.alias("FakeModel", FakeModel.class);
		xstream.alias("FakeView", FakeView.class);
		xstream.alias("FakeController", FakeController.class);
		xstream.alias("Key", Key.class);
		
		//register converters
		xstream.registerConverter(new FakeModelConverter());
		//xstream.registerConverter(new ModelConverter());
		xstream.registerConverter(new FakeControllerConverter());
		
		//-----------------------------------------------------------
		//build, save a model
		
		//build objects
//		FakeModel model = new FakeModel();
//		
//		//FakeView view = new FakeView();
//		//model.attach(view);
//		FakeController controller = new FakeController(events);
//		controller.setTestDatum(3);
//		controller.setModel(model);
//		model.setController(controller);
//		
//		
//		
//		//Save to file
//		file = new File(Constants.testPath + "//FakeModel.xml");		
//		FileOutputStream fos = new FileOutputStream(file);
//		OutputStreamWriter osw = new OutputStreamWriter(fos);
//		ObjectOutputStream out = xstream.createObjectOutputStream(osw);		
//		out.writeObject(model);	
//		out.writeObject(controller);
//		out.close();
		//-----------------------------------------------------------
		
		
		
		//-----------------------------------------------------------
		//load model from XML
		file = new File(Constants.testPath + "//FakeModel.xml");
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis);
		ObjectInputStream in = xstream.createObjectInputStream(isr);
			
		FakeModel model2 = (FakeModel) in.readObject();
		//FakeModel model2 = (FakeModel) obj;		
		System.out.println(model2.getKeyValue());
		
		FakeController controller2 = (FakeController) in.readObject();
		//FakeController controller2 = (FakeController) obj;
		System.out.println(controller2.getKey().getValue());
		System.out.println(controller2.getTestDatum());
		
		
		
		//-----------------------------------------------------------
		
		
		
		
		
		//-----------------------------------------------------------
		//compare
		
		//-----------------------------------------------------------
		
		
		return false;
	}

}
